<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<meta name="format-detection" content="telephone=no, email=no, date=no, address=no">
		<title>Mocc讨论章节列表页</title>
		<link rel="stylesheet" type="text/css" href="../../css/mui.min.css">
		<link rel="stylesheet" type="text/css" href="../../css/weui.min.css">
		<link rel="stylesheet" type="text/css" href="../../css/discuss.css">
		<link rel="stylesheet" href="../../css/CFNetPP_Base.css">
		<link rel="stylesheet" href="../../css/base.css">
		<style>
			.weui_mask {
				position: fixed;
				z-index: 998;
				top: 0;
				right: 0;
				bottom: 0;
				left: 0;
			}
			.weui_dialog {
				z-index: 999;
			}
			.myScore2 {
				background: #fff;
				padding: 20px;
				border-bottom: 1px solid #d9d9d9;
				border-top: 1px solid #d9d9d9;
				/* margin-top: 10px; */
			}
			html {
				background: #F3F5F7
			}
			.base_color {
				background: #F3F5F7
			}
		</style>
	</head>
	<body>
		<div id="main_div ">
			<div class=" writeReport " >
				<div class="bgwhite  "  >
					<div class="padding10" id="bewriteDIV" hidden="hidden" >
						<p class=" base_black_grey base_title_font">
							描述
						</p>
						<div class="base_dark_grey base_content_font according-2-rows text_indent_2 word-break" id="bewrite" onclick="openBewrite()"></div>
					</div>
					<div class="div_divider"></div>
					<div style="background-color: #f0f0f0;height:15px"></div>
					<div class="myScore2  sf_peix">
						<span class="font16 base_dark_grey "style='color: red'id="num">0<span class="font16 base_dark_grey left_5">条</span></span>
						<span class="font16 base_dark_grey left_10" id="theme">讨论</span>
					</div>
				</div>
				<div id="commentList">
					<ul class="mui-table-view "id="list_discus"></ul>
					<div class="noSign base_color" hidden="hidden" id="noData" >
						<img src="../../image/icon_img.png" width="60%">
						<p class="font16 top_15 base_dark_grey" >
							暂无讨论
						</p>
					</div>
					<div class="loadFailure base_color" hidden="hidden" id="noNet">
						<img src="../../image/icon_img01.png" width="60%">
						<p class="" >
							数据加载失败
						</p>
						<p>
							请检查您的网络
						</p>
						<button type="button" class="mui-btn mui-btn-outlined" onclick="RefreshData()">
							重新加载
						</button>
					</div>
				</div>
				<div id="offlineAlert" class="weui_dialog_confirm" style="display: none;">
					<div class="weui_mask"></div>
					<div class="weui_dialog">
						<div class="weui_dialog_hd">
							<strong class="weui_dialog_title">确定删除此评论？</strong>
						</div>
						<div class="weui_dialog_bd"></div>
						<div class="weui_dialog_ft">
							<a onclick="cleanCancel()" href="#" class="weui_btn_dialog default"  style="color:#18b4ed">取消</a>
							<a onclick="cleanConfirm()" href="#" class="weui_btn_dialog primary"  style="color:#18b4ed">确定</a>
						</div>
					</div>
				</div>
			</div>
	</body>
	<script type="text/javascript" src="../../script/jquery.min.js"></script>
	<script type="text/javascript" src="../../script/api.js"></script>
	<script src="../../script/jquery.min.js" type="text/javascript"></script>
	<script type="text/javascript" src="../../script/MD5.js"></script>
	<script src="../../script/common.js"></script>
	<script type="text/javascript" src="../../script/requestTool.js"></script>
	<script type="text/javascript">
		var page = 1;
		var row = 10;
		var refreshCount = 0;
		//更新数据条数
		var dataArr = new Array();
		var pid = "";
		// 计划id
		var cid = "";
		//课程id
		var chapterid = "";
		//章节id
		var title = "";
		var is_dis_bewrite = '';
		//被点击的题目id
		var Itemid = "";
		var dataLen = 0;
		apiready = function() {
			var pageParam = api.pageParam;
			pid = pageParam.pid;
			cid = pageParam.cid;
			chapterid = pageParam.chapterid;
			is_dis_bewrite = api.pageParam.is_dis_bewrite
			title = pageParam.title;
			//接收通知
			ReceiveNotifi();
			bewrite();
			//开启加载框
			showProgress();
			//加载数据
			loadNewData();
			//下拉加载
			setRefreshHeader();
			//上拉刷新
			setPullUpRefresh();
		};
		function bewrite() {
			pageParam = api.pageParam;
			$('#nav_title').html(pageParam.title);
			var bewrite = pageParam.bewrite;
			if (bewrite != "" && bewrite != null && bewrite != undefined) {
				$('#bewriteDIV').show();
				if (removeHTMLTag(bewrite).length > 0) {
					$('#bewrite').html(removeHTMLTag(bewrite));
				} else {
					$('#bewrite').text('点击查看详情');
				}
			}
		}

		function openBewrite() {
			api.openWin({useWKWebView:true,
				name : 'Student_Free_bewrite',
				url : '../Study/Student_Free_bewrite.html',
				pageParam : {
					cdetailtitle : api.pageParam.title,
					bewrite : api.pageParam.bewrite,
					is_dis_bewrite : api.pageParam.is_dis_bewrite
				}
			});
		}

		function backyes() {
			$('#backAlert').hide();
			api.closeWin({
			});
		}

		function backCancel() {
			$('#backAlert').hide()
		}

		//接收通知刷新列表
		function ReceiveNotifi() {
			//编辑讨论的通知
			api.addEventListener({
				name : 'RefreshMoccDiscusListNotifi'
			}, function(ret, err) {
				loadNewData();
			});
			//添加新的讨论的通知
			api.addEventListener({
				name : 'RefreshMoccAddDiscusListNotifi'
			}, function(ret, err) {
				loadNewData();
			});
		}

		function loadNewData() {
			page = 1;
			loadData();
			//停止刷新控件
		}

		//		接收通知
		function loadMoreData() {
			page++;
			loadData();
		}

		function loadData() {
			var param = {
				pid : pid,
				cid : cid,
				vid : chapterid,
				jid : $api.getStorage('cfnetppjxid'),
				uid : $api.getStorage('cfnetppuid'),
				page : page,
				rows : row,
			};
			cfnetppPOST(url_discussList, param, true, function(data, status) {
				//							alert(JSON.stringify(param));
				//							alert(JSON.stringify(data));
				api.refreshHeaderLoadDone();
				hideProgress();
				if (status == 'success') {
					$('#noNet').hide();
					if (data.code == 200) {
						$('#noData').hide();
						var list = data.json.list;
						dataLen = data.json.total;
						$('#num').text(dataLen);
						if (list != null && list.length > 0) {
							refreshCount = list.length;
							var listArr = new Array();
							var htmlString = new String();
							if (page == 1) {
								for (var i = 0; i < list.length; i++) {
									var weeklyData = list[i];
									var data = {
										uid : weeklyData.uid,
										id : weeklyData.id,
										userimg : weeklyData.picurl,
										username : weeklyData.rename,
										content : weeklyData.content,
										title : weeklyData.title,
										num : weeklyData.num,
										ctime : weeklyData.ctime
									};
									listArr.push(data);
									htmlString += htmlStringWithName(data, i);
								}
								dataArr = listArr;
								$('#list_discus').html(htmlString);
							} else {
								var oldCount = dataArr.length;
								for (var i = 0; i < list.length; i++) {
									var index = oldCount + i;
									var weeklyData = list[i];
									var data = {
										uid : weeklyData.uid,
										id : weeklyData.id,
										userimg : weeklyData.picurl,
										username : weeklyData.rename,
										content : weeklyData.content,
										title : weeklyData.title,
										num : weeklyData.num,
										ctime : weeklyData.ctime
									};
									listArr.push(data);
									htmlString += htmlStringWithName(data, index);
								}
								dataArr = dataArr.concat(listArr);
								$('#list_discus').append(htmlString);
							}
						} else {
							if (page == 1) {
								$('#noData').show();
							} else {
								api.toast({
									msg : '暂无数据',
									duration : 2000,
									location : 'bottom'
								});
							}
							$('#list_discus').html('');
						}
					} else if (data.code == 108) {
						if (page == 1) {
							$('#num').text('0');
							$('#list_discus').html('');
							$('#noData').show();
						}
						//
					} else {
						if (page == 1) {
							$('#list_discus').html('');
							$('#noData').show();
						}
					}
				} else {
					$('#noNet').show();
				}
			});
		}

		function htmlStringWithName(list, index) {
			var spanStr = '';
			var imgStr = '';
			if (list.userimg == null || list.userimg == '') {
				imgStr = '../../icon/icon_default_HeadPortrait.png';
			} else {
				imgStr = list.userimg;
			}
			if (list.uid == $api.getStorage('cfnetppuid')) {
				//				spanStr = '<td class="Retweet" onclick="EditDiscus(' + index + ')"><img class="incon_samll" src="../../image/icon_bianji.png"/> <span class="left_5">编辑</span></td>' +
				spanStr = '<td class="Retweet" onclick="showAlert(' + list.id + ')"><img class="incon_samll" src="../../image/icon_shanchu.png"/ ><span class="left_5">删除</span></td>' + '<td class="Retweet" id="Reply" onclick="ReplayDiscus(' + index + ')"><img  src="../../image/icon_huifu.png"  class="incon_samll"><span class="left_5">' + list.num + '</span></td>';
			} else {
				spanStr = '<td class="Comment" id="Reply" onclick="ReplayDiscus(' + index + ')"><img  src="../../image/icon_huifu.png"  class="incon_samll"><span class="left_5">' + list.num + '</span></td>';
			}
			return '<li dataid="' + index + '" class="mui-table-view-cell mui-media list_item" onclick="openReplayWin(' + index + ')">' 
			+ '<div >' 
			+ '<img class="mui-media-object mui-pull-left cell_user_icon" src="' + imgStr + '" style="display: block">' 
			+ '<p class=" base_content_font base_dark_grey" >' + '<span>' + list.username + '</span>' 
			+ '<span class="left_5 font10">' + timestampFormat(list.ctime) + '</span>' + '</p>' 
			+ '<div class="Commentbody top_2 ">' 
			+ '<p class="word-break left_50 font12">' + list.content + '</p>' 
			
			
			+ '</div>' + '</div>' + '<table class="MBToolBar top_5 ">' + '<tr>' + spanStr + '</tr>' + '</table>' + '</li>';
		}

		function showAlert(sid) {
			event.stopPropagation();
			$('#offlineAlert').show();
			//			alert(sid)
			Itemid = sid;
		}

		function cleanCancel() {
			$('#offlineAlert').hide();
		}

		//确定
		function cleanConfirm() {
			$('#offlineAlert').hide();
			deleteDiscus(Itemid);
			Itemid = "";
		}

		//删除评论
		function deleteDiscus(Itemid) {
			var param = {
				id : Itemid,
				uid : $api.getStorage('cfnetppuid'),
				jid : $api.getStorage('cfnetppjxid'),
				pid : pid,
				cid : cid,
				vid : chapterid,
			};
			cfnetppPOST(url_deleteDiscuss, param, true, function(data, status) {
				hideProgress();
				if($api.getStorage('cfnetSelectType') == 0) {//学生身份){
				  SendBusinessOperationsLogNotifi('讨论列表页', '020', param, param, '', data, 'Mocc课程讨论章节学生删除讨论接口', '020-03');
				}else if($api.getStorage('cfnetSelectType') == 1){//老师身份
				  SendBusinessOperationsLogNotifi('讨论列表页', '020', param, param, '', data, 'Mocc课程讨论章节学生删除讨论接口', '020-08');
				}
				if (status == 'success') {
					if (data.code == 200) {
						ShowToast('删除成功');
						Itemid = "";
						loadNewData();
					} else {
						api.toast({
							msg : '删除失败',
							duration : 2000,
							location : 'bottom'
						});
					}
				} else {
					api.toast({
						msg : '获取数据失败',
						duration : 2000,
						location : 'bottom'
					});
				}
			});
		}

		//编辑评论
		function EditDiscus(index) {
			event.stopPropagation();
			var data = dataArr[parseInt(index)];
			var id = data.id;
			var content = data.content;
			api.openFrame({
				name : 'MoccCourse_EditDiscus',
				url : 'MoccCourse_EditDiscus.html',
				pageParam : {
					id : id,
					pid : pid,
					title : title,
					chapterid : chapterid,
					content : content,
					type : 0//mocc
				}
				//
			});
		}

		//回复讨论
		function ReplayDiscus(index) {
		}

		//列表条目点击
		function openReplayWin(index) {
			var data = dataArr[parseInt(index)];
			var id = data.id;
			var userimg = data.userimg;
			api.openWin({useWKWebView:true,
				name : 'New_Student_Study_DiscussChapterReplayWindow',
				url : 'New_Student_Study_DiscussChapterReplayWindow.html',
				pageParam : {
					data : data,
					title : title,
					pid : pid,
					cid : cid,
					chapterid : chapterid,
					type : 1,
					sourceurl : api.pageParam.sourceurl,
					sourcetitle : api.pageParam.sourcetitle,
				}
			});
		}
	</script>
</html>